package cn.openea.perpartner.meily.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.constant.Constants;
import cn.openea.perpartner.common.redis.service.RedisCache;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Config;
import cn.openea.perpartner.meily.web.entity.dto.RemoveDto;
import cn.openea.perpartner.meily.web.mapper.ConfigMapper;
import cn.openea.perpartner.meily.web.service.IConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 参数配置表(MerryConfig)表服务实现类
 *
 * @author Weird077
 * @since 2022-12-01 18:22:40
 */
@Slf4j
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements IConfigService {
    @Autowired
    private RedisCache redisCache;
    @Value("${meily.redis.expire.config}")
    private long expireTime;

    @Override
    public List<Config> selectList(Config config) {
        List<Config> configList = new ArrayList<>();
        if (StrUtil.isNotBlank(config.getConfigType())) {
            configList = redisCache.getCacheList(getCacheKey(config.getConfigType()));
        }
        if (!configList.isEmpty()) {
            return configList;
        }
        LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(config.getConfigType())) {
            wrapper.eq(Config::getConfigType, config.getConfigType());
        }
        wrapper.orderByAsc(Config::getSort);
        wrapper.orderByDesc(Config::getUpdateTime);
        configList = list(wrapper);
        if (!configList.isEmpty() && StrUtil.isNotBlank(config.getConfigType())) {
            redisCache.setCacheList(getCacheKey(config.getConfigType()), configList);
            redisCache.expire(getCacheKey(config.getConfigKey()), expireTime, TimeUnit.MINUTES);
        }
        return configList;
    }

    @Override
    public boolean insert(Config config) {
        String key = getCacheKey(config.getConfigType());
        if (Boolean.TRUE.equals(redisCache.hasKey(key))) {
            redisCache.deleteObject(key);
        }
        return saveOrUpdate(config);
    }

    @Override
    public boolean delete(RemoveDto removeDto) {
        removeDto.getTypeList().forEach(type -> {
            if (Boolean.TRUE.equals(redisCache.hasKey(getCacheKey(type)))) {
                redisCache.deleteObject(getCacheKey(type));
            }
        });
        return removeByIds(removeDto.getIdList());
    }

    @Override
    public Config selectByKey(String configKey) {
        LambdaQueryWrapper<Config> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Config::getConfigKey, configKey);
        return getOne(wrapper, false);
    }

    @Override
    public boolean copy(String configId) {
        Config config = getById(configId);
        String key = getCacheKey(config.getConfigType());
        if (Boolean.TRUE.equals(redisCache.hasKey(key))) {
            redisCache.deleteObject(key);
        }
        config.setConfigId(null);
        config.setConfigName(config.getConfigName() + "-副本");
        config.setCreateUser(UserContext.getUserId());
        config.setUpdateUser(UserContext.getUserId());
        return saveOrUpdate(config);
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return Constants.MERRY_CONFIG_KEY + configKey;
    }

}

